package tp.pr3;

import tp.pr3.items.Item;
import tp.pr3.items.ItemContainer;

/**
 * Representa un lugar en la ciudad. Los lugares pueden estar conectados a
 * traves de calles en las cuatro direcciones cardinales. Cada lugar tiene su
 * nombre y una descripcion, que se muestra cuando el robot llega al lugar. Un
 * lugar puede representar ademas una nave espacial, donde el robot esta a
 * salvo. Cuando el robot llega a la nave espacial, se apaga y el juego termina.
 */
public class Place {
	private static final String LINE_SEPARATOR = System
			.getProperty("line.separator");
	private String name;
	private String description;
	private boolean isSpaceShip;
	private ItemContainer items;

	/**
	 * Constructor de un lugar
	 * 
	 * @param dos
	 *            strings: el nombre y la descripcion, y un boolean indicando si
	 *            el lugar ademas es nave espacial
	 */
	public Place(String name, boolean isSpaceShip, String description) {
		this.name = name;
		this.isSpaceShip = isSpaceShip;
		this.description = description;
		this.items = new ItemContainer(); // Nuevo contenedor de items vac�o
	}

	/**
	 * Es nave espacial?
	 * 
	 * @return true si lo es, false en caso contrario
	 */
	public boolean isSpaceship() {
		return this.isSpaceShip;
	}

	/**
	 * Overrides toString method. Returns the place name, its description and
	 * the list of items contained in the place
	 * 
	 * @return un string con el nombre y la descripcion del lugar
	 */
	public String toString() {

		if (items.numberOfItems() > 0)
			return this.name + LINE_SEPARATOR + this.description
					+ LINE_SEPARATOR + Message.PLACE_CONTAINS + LINE_SEPARATOR
					+ items.toString();
		else
			return this.name + LINE_SEPARATOR + this.description
					+ LINE_SEPARATOR + Message.EMPTY_PLACE + LINE_SEPARATOR;
	}

	/**
	 * Tries to pick an item characterized by a given identifier from the place.
	 * If the action was completed the item must be removed from the place
	 * 
	 * @param id
	 *            - The identifier of the item
	 * @return The item of identifier id if it exists in the place. Otherwise
	 *         the method returns null
	 */
	public Item pickItem(java.lang.String id) {
		return items.pickItem(id);
	}

	/**
	 * Tries to add an item to the place. The operation can fail (if the place
	 * already contains an item with the same name)
	 * 
	 * @param it
	 *            - The item to be added
	 * @return true if and only if the item can be added to the place, i.e., the
	 *         place does not contain an item with the same name
	 */
	public boolean addItem(Item it) {
		return items.addItem(it);
	}

	/**
	 * Drop an item in this place. The operation can fail, returning false
	 * 
	 * @param it
	 *            - The name of the item to be dropped.
	 * @return true if and only if the item is dropped in the place, i.e., an
	 *         item with the same identifier does not exists in the place
	 */
	public boolean dropItem(Item it) {
		return items.addItem(it);
	}

	/**
	 * Checks if an item is in this place
	 * 
	 * @param id
	 *            - Identifier of an item
	 * @return true if and only if the place contains the item identified by id
	 */
	public boolean existItem(java.lang.String id) {
		return (items.getItem(id) != null);
	}
}
